// 宠物用户的相关接口,包括新增用户,删除用户,查询用户,模糊查询用户
const express = require("express")
const router = express.Router()
// 引入连接池
const pool = require("../pool/pool")
// 引入封装后的响应
const Response = require('../utils/Response')
// 引入表单验证部分
const Joi = require("joi")
//查询接口
router.get("/pet-user", (req, res) => {
  // 表单验证
  let schema = Joi.object({
    page: Joi.number().required(),
    pagesize: Joi.number().integer().max(50).required(),
  })
  let { error, value } = schema.validate(req.query)
  if (error) {
    res.send(Response.error(error))
    return
  }
  // 执行查询业务
  let { page, pagesize } = req.query
  let startIndex = (page - 1) * 10
  let size = parseInt(pagesize)
  let sql = "select * from user limit ?,?"
  pool.query(sql, [startIndex, size], (error, result) => {
    if (error) {
      throw error
    }
    res.send(Response.ok(result))
  })
})
// 添加用户接口
// 参数有:用户姓名,邮箱,密码
router.post('/pet-user/add',(req,res)=>{
  let {userName,useEmail,userPwd} = req.body
  // 表单验证
  let schema = Joi.object({
    userName: Joi.string().required(),
    useEmail: Joi.string().email().required(),
    userPwd: Joi.string().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(error))
    return
  }
  // 添加操作
let sql = 'insert into user (u_name,u_email,u_pwd) values (?,?,?)'
pool.query(sql,[userName,useEmail,userPwd],(err,result)=>{
  if(err){
    res.send(Response.error(500,err))
    throw err
  }
  res.send(Response.ok())
})
})
// 删除用户Id
router.post("/pet-user/del", (req, res) => {
  let { u_id } = req.body
  let schema = Joi.object({
    u_id: Joi.string().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(400, error))
    return
  }
  let sql = "delete from user where u_id=?"
  pool.query(sql, [u_id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error))
      throw error
    }
    res.send(Response.ok())
  })
})
// 通过用户ID查询用户
router.post("/pet-user/u_id", (req, res) => {
  let { u_id } = req.body
  let schema = Joi.object({
    u_id: Joi.number().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(400, error))
    return
  }
  let sql = "select * from user where u_id=?"
  pool.query(sql, [u_id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error))
      throw error
    }
    if (result && result.length == 0) {
      res.send(Response.ok(null))
    } else {
      res.send(Response.ok(result[0]))
    }
  })
})
// 修改用户密码接口
router.post('/pet-user/update',(req,res)=>{
  let {u_name,u_email,u_pwd,u_id} = req.body
  let schema = Joi.object({
    u_name: Joi.string().required(),
    u_email: Joi.string().required(),
    u_pwd: Joi.string().required(),
    u_id: Joi.number().required(),
  })
  let { error, value } = schema.validate(req.body)
  if (error) {
    res.send(Response.error(error))
    return
  }
  let sql = `update user set u_name=?,u_email=?,u_pwd=? where u_id=?`;
  pool.query(sql,[u_name,u_email,u_pwd,u_id],(err,result)=>{
    if(err){
      res.send(Response.error(500,err))
    throw err
    }
    res.send(Response.ok())
  })

})
module.exports = router;
